package com.bw.day03.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.base.net.entity.LoginEntity
import com.bw.base.net.entity.Res
import com.bw.base.net.entity.SendCodeEntity
import com.bw.base.net.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class LoginViewModel:BaseViewModel() {
    val sendSuccess=MutableLiveData<Res<SendCodeEntity>>()
    val sendFail=MutableLiveData<String>()
    fun sendCode(phone:String){
        viewModelScope.launch {
            try {
                sendSuccess.value=apiService.sendCode(phone)
            } catch (e:Exception){
                sendFail.value=e.message
            }

        }
    }
    val codeSuccess=MutableLiveData<Res<LoginEntity>>()
    val codeFail=MutableLiveData<String>()
    fun loginCode(captcha:String,phone:String){
        viewModelScope.launch {
            try {
                val map= mutableMapOf<String,Any>()
                map["captcha"]=captcha
                map["phone"]=phone
                codeSuccess.value=apiService.loginCode(mapToBody(map))
            }catch (e:Exception){
                codeFail.value=e.message
            }
        }
    }
}